/**
 * Created by 小虎Oni on 2016/9/27.
 */
var gulp = require('gulp');
var watch = require('gulp-watch');//监听
var uglify = require('gulp-uglify');//压缩
var streamify = require('gulp-streamify');
var sass = require('gulp-sass');//sass
var plumber = require('gulp-plumber');//事件
var notify = require('gulp-notify');//错误
var browserify = require('browserify');
var babelify = require('babelify');
var source = require('vinyl-source-stream');
gulp.task('watchSource', function () {
    return watch('source/**/*', function () {
        browserify('./source/app.js')
            .transform(babelify,{presets:["react"]})
            .bundle()
            .on('error', function(err){
                var args = Array.prototype.slice.call(arguments);
                notify.onError({
                    title: "Compile Error",
                    message: "<%= error.message %>"
                }).apply(this, args);
                this.emit('end');
            })
            .pipe(source('zb.js'))
            .pipe(gulp.dest('./build/'))
            .on('end', function () {
                console.log('update js success')
            });
    })
});
gulp.task('build', function () {
    return browserify('./source/app.js')
        .transform(babelify,{presets:["react"]})
        .bundle()
        .pipe(source('zb.js'))
        .pipe(streamify(uglify('zb.js')))
        .pipe(gulp.dest('./build/'));
});
gulp.task('buildCSS', function () {
    gulp.src('./style/app.scss')
    .pipe(plumber({errorHandler: notify.onError('Error: <%= error.message %>')}))
    .pipe(sass())
    .pipe(gulp.dest('./build'))
})
gulp.task('watchStyle', function () {
    gulp.watch('./style/**/*',['buildCSS'])
})
gulp.task('default',['watchSource','watchStyle'])